const App = getApp()
Page({
  data: {
    pageShow: true,
    setInter: '', //定时器
    src: '',
    hasorder: '',
    selectIdx:'',
  },
  onLoad: function (options) {

  },
  onReady: function () {
    this.audioCtx = wx.createAudioContext('myAudio')
  },
  selectTap(e){
    var that = this;
    let idx = e.currentTarget.dataset.idx
    this.setData({
      selectIdx:idx,
      src: idx == 1 ? "https://jiayou.ziming.work/start.mp3" :  "https://jiayou.ziming.work/stop.mp3",
    },()=>{
      console.log(idx)
      App.api.$http({
        url: 'Index/changeWorking',
        method: 'POST',
        data: {
          isworking: idx,
        }
      }, {}).then(res => {
        console.log(res)
        that.audioCtx.play()
      })
    })
  },
  onShow: function () {
    let that = this
    that.info()
    that.getLocation()
    that.data.setInter = setInterval(
      function () {
        that.getLocation()
      }, 10000);
  },
  // 实时更新司机经纬度
  getLocation() {
    let that = this
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        App.api.$http({
          url: 'Index/updateLocation',
          method: 'POST',
          data: {
            longitude: res.longitude,
            latitude: res.latitude
          }
        }, {}).then(res => {
          if (res.data.hasorder == 1) {
            that.info()
          }
          that.setData({
            hasorder: res.data.hasorder,
            src: res.data.mp3
          })
          console.log()
          if (res.data.has) {
            that.audioCtx.play()
          }
        })
      }
    })
  },
  info() {
    let that = this
    App.api.$http({
      url: 'user/staff',
      method: 'get',
      data: {}
    }, {}).then(res => {
      if (res) {
        that.setData({
          info: res.data,
          selectIdx:res.data.userInfo.isworking
        })
        console.log(that.data.info)
      }
    })
  },
  onHide: function () {
    let that = this
    clearInterval(that.data.setInter)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    clearInterval(that.data.setInter)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})